﻿using Application.CodeRuleService;
using Application.PositionService;
using Application.RoleService;
using Application.UserService;
using Domain.Entity;
using Domain.Entity.zct;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WMSManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService roleService;

        public RoleController(IRoleService roleService)
        {
            this.roleService = roleService;
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Role>> GetAll()
        {
            return await roleService.GetAll();
        }

        /// <summary>
        /// 查询所有:分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<FenYe<Role>> GetFenYe(int CurrentPage, string? Role_code, string? Role_name, int? Role_status)
        {
            FenYe<Role> fenYe = new FenYe<Role>()
            {
                CurrentPage = CurrentPage,
            };
            return await roleService.GetAll(fenYe, Role_code, Role_name, Role_status);
        }

        /// <summary>
        /// id查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Role> GetID(int id)
        {
            return await roleService.GetID(id);
        }

        /// <summary>
        /// 停用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetDeactivated(int id)
        {
            return roleService.GetDeactivated(id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<IActionResult> GetDelete(int id)
        {
            try
            {
                var result = await roleService.GetDelete(id);

                // 根据 Repository 返回的 int 结果，返回不同的 HTTP 响应
                return result switch
                {
                    1 => Ok(new { code = 1, message = "角色删除成功" }),
                    0 => BadRequest(new { code = 0, message = "删除失败：该角色已被其他单据引用，无法删除" }),
                    -1 => NotFound(new { code = -1, message = "角色不存在或已被删除" }),
                    _ => StatusCode(500, new { code = -2, message = "服务器内部错误：未知的删除结果" })
                };
            }
            catch (System.Exception ex)
            {
                // 记录异常日志 (如果配置了日志系统)
                // _logger.LogError(ex, "删除角色 {SupplierId} 时发生异常", id);
                return StatusCode(500, new { code = -2, message = $"服务器内部错误: {ex.Message}" });
            }
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetEnable(int id)
        {
            return roleService.GetEnable(id);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="addRolesWithPermissionsDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public Task<int> GetAdd(AddRolesWithPermissionsDto addRolesWithPermissionsDto)
        {
            return roleService.GetAdd(addRolesWithPermissionsDto);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="updateRolesWithPermissionsDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> GetEdit(UpdateRolesWithPermissionsDto updateRolesWithPermissionsDto)
        {
            return roleService.GetEdit(updateRolesWithPermissionsDto);
        }
    }
}
